//
//  ZMBWalletView.swift
//  HengYouCai
//
//  Created by apple on 2018/5/9.
//  Copyright © 2018年 lwj. All rights reserved.
//

import UIKit
import SnapKit
class ZMBWalletView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = Normal_458Color
        let bgView = UIView()
        bgView.backgroundColor = UIColor.white
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(180)
        }
        // 添加卡片图片
        bgView.addSubview(cardImgView)
        cardImgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsetsMake(10, 15, 10, 15))
        }
        // 总资产
        bgView.addSubview(allAssetsTextLab)
        allAssetsTextLab.snp.makeConstraints { (make) in
            make.top.equalTo(45)
            make.centerX.equalToSuperview()
        }
        bgView.addSubview(eyeBtn)
        eyeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(allAssetsTextLab)
            make.left.equalTo(allAssetsTextLab.snp.right).offset(9)
            make.height.width.equalTo(23)
        }
        bgView.addSubview(allAssetsLab)
        allAssetsLab.snp.makeConstraints { (make) in
            make.top.equalTo(allAssetsTextLab.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
        }
        
        
        // 白色竖线
        bgView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(allAssetsTextLab.snp.bottom).offset(20)
            make.size.equalTo(CGSize(width: 0.5, height: 30))
            make.centerX.equalToSuperview()
        }
        // 累计收益
        let totalInterestView = UIView()
        totalInterestView.backgroundColor = UIColor.clear
        bgView.addSubview(totalInterestView)
        totalInterestView.snp.makeConstraints { (make) in
            make.height.equalTo(43)
            make.left.equalTo(15)
            make.bottom.equalTo(-10)
            make.right.equalTo(-15)
        }
        totalInterestView.addSubview(totalInterestValue)
        totalInterestValue.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.right.equalToSuperview()
            make.left.equalToSuperview()
        }
//        totalInterestView.addSubview(totalInterestLab)
//        totalInterestLab.snp.makeConstraints { (make) in
//            make.top.equalToSuperview()
//            make.bottom.equalToSuperview()
//            make.left.equalToSuperview()
//            make.right.equalTo(lineView.snp.left).offset(-5)
//        }
        
       
        
        let bottomBgView = UIView()
        bottomBgView.backgroundColor = UIColor.white
        self.addSubview(bottomBgView)
        bottomBgView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(188)
            make.height.equalTo(75)
        }
        // 账户余额
//        let availableAmountView = UIView()
//        availableAmountView.backgroundColor = UIColor.clear
//        bottomBgView.addSubview(availableAmountView)
//        availableAmountView.snp.makeConstraints { (make) in
//            make.top.equalTo(20)
//            make.left.equalTo(15)
//            make.bottom.equalTo(-20)
//            make.width.equalTo(200)
//        }
        bottomBgView.addSubview(availableAmountValue)
        availableAmountValue.snp.makeConstraints { (make) in
            make.top.equalTo(15)
            make.bottom.equalTo(-15)
            make.left.equalTo(15)
        }
//        availableAmountView.addSubview(availableAmountLab)
//        availableAmountLab.snp.makeConstraints { (make) in
//            make.bottom.equalToSuperview()
//            make.centerX.equalToSuperview()
//        }
//
        // 充值提现
        bottomBgView.addSubview(rechargeBtn)
        bottomBgView.addSubview(withdrawalBtn)
        rechargeBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.centerY.equalToSuperview()
            make.width.equalTo(75)
            make.height.equalTo(28)
            
        }
        withdrawalBtn.snp.makeConstraints { (make) in
            make.right.equalTo(rechargeBtn.snp.left).offset(-10)
            make.centerY.equalToSuperview()
            make.width.equalTo(75)
            make.height.equalTo(28)
        }
//        reloadMyWallet()
        self.addSubview(self.authenticationButton)
        authenticationButton.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(bottomBgView.snp.bottom).offset(5)
            make.height.equalTo(78)
            
//            make.top.equalTo(menuCollectionView.snp.bottom).offset(7)
//            make.left.equalToSuperview()
//            make.right.equalToSuperview()
//            make.height.equalTo(78)
            
        }
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func openOrCloseEye() {
        let result = UserDefaults.standard.object(forKey: KWalletaward)
        let myWalletaward = JSON(result ?? "")
        //累计收益
        let totalProfitLabel = String(format: "%@:  ", myWalletaward["totalProfitLabel"].string!)
        //账户余额
        let balanceLabel = myWalletaward["balanceLabel"].string
        
        eyeBtn.isSelected = !eyeBtn.isSelected
        UserDefaults.standard.set(eyeBtn.isSelected, forKey: "isBiyan")
        if eyeBtn.isSelected {
            allAssetsLab.text = "****"
            totalInterestValue.attributedText = handlerRichtext(totalProfitLabel, "****", "元", UIColor.white, UIColor.white, UIColor.white, Font_WL(13), BoldFont_WL(14), BoldFont_WL(14))
            
//            let amountValue = handlerRichtext("账户余额(元)", "\n", "****", Normal_999Color, Normal_999Color, Normal_333Color, Font_WL(14), Font_WL(14), BoldFont_WL(15))
//            let paragraphStyle = NSMutableParagraphStyle()
//            paragraphStyle.lineSpacing = 10
//            amountValue.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: 0, length: amountValue.length))
            availableAmountValue.attributedText = handlerRichtext(balanceLabel!, "\n", "****", Normal_999Color, Normal_999Color, Normal_333Color, Font_WL(14), Font_WL(14), BoldFont_WL(15))
        }else{
            let result = UserDefaults.standard.object(forKey: KWalletaward)
            if result != nil {
                let myWalletaward = JSON(result ?? "")
                allAssetsLab.text = myWalletaward["allAssets"].stringValue == "" ? "0.00" : myWalletaward["allAssets"].stringValue
                 totalInterestValue.attributedText = handlerRichtext(totalProfitLabel, (myWalletaward["totalInterest"].stringValue == "" ? "0.00" : myWalletaward["totalInterest"].stringValue), "元", UIColor.white, UIColor.white, UIColor.white, Font_WL(13), BoldFont_WL(14), BoldFont_WL(14))
//                let amountValue = handlerRichtext("账户余额(元)", "\n", myWalletaward["availableAmount"].stringValue == "" ? "0.00" : myWalletaward["availableAmount"].stringValue, Normal_999Color, Normal_999Color, Normal_333Color, Font_WL(14), Font_WL(14), BoldFont_WL(15))
//                let paragraphStyle = NSMutableParagraphStyle()
//                paragraphStyle.lineSpacing = 10
//                amountValue.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: 0, length: amountValue.length))
                availableAmountValue.attributedText = handlerRichtext(balanceLabel!, "\n", myWalletaward["availableAmount"].stringValue == "" ? "0.00" : myWalletaward["availableAmount"].stringValue, Normal_999Color, Normal_999Color, Normal_333Color, Font_WL(14), Font_WL(14), BoldFont_WL(15))
            }
        }
    }
    func reloadMyWallet() {
    
        let result = UserDefaults.standard.object(forKey: KWalletaward)
        if  result != nil {
            let  myWalletaward = JSON(result as Any)
            //累计收益
            let totalProfitLabel = String(format: "%@:  ", myWalletaward["totalProfitLabel"].stringValue)
            //账户余额
            let balanceLabel = myWalletaward["balanceLabel"].stringValue
            //总资产
            let totalAssetsLabel = myWalletaward["totalAssetsLabel"].stringValue
            //提现
            let withdrawButtonLabel = myWalletaward["withdrawButtonLabel"].stringValue
            //充值
            let rechargeButtonLabel = myWalletaward["rechargeButtonLabel"].stringValue
            
            //充值提现 总资产
            self.rechargeBtn.setTitle(rechargeButtonLabel, for: .normal)
            self.withdrawalBtn.setTitle(withdrawButtonLabel, for: .normal)
            self.allAssetsTextLab.text = totalAssetsLabel
            //实名认证 按钮
            let imageUrl = URL(string: myWalletaward["bindingImageUrl"].stringValue)
//            self.authenticationButton.kf.setImage(with: ImageResource(downloadURL: imageUrl!), for: .normal)
            self.authenticationButton.kf_setImage(with: ImageResource(downloadURL: imageUrl!), for: .normal)
            
            
            if eyeBtn.isSelected {
                allAssetsLab.text = "****"
                totalInterestValue.attributedText = handlerRichtext(totalProfitLabel, "****", "元", UIColor.white, UIColor.white, UIColor.white, Font_WL(13), BoldFont_WL(14), BoldFont_WL(14))
                availableAmountValue.attributedText = handlerRichtext(balanceLabel, "\n", "****", Normal_999Color, Normal_999Color, Normal_333Color, Font_WL(14), Font_WL(14), BoldFont_WL(15))
            }else{
                
                if result != nil {
                    
                    allAssetsLab.text = myWalletaward["allAssets"].stringValue == "" ? "0.00" : myWalletaward["allAssets"].stringValue
                    //                totalInterestValue.text = myWalletaward["totalInterest"].stringValue == "" ? "0.00" : myWalletaward["totalInterest"].stringValue
                    totalInterestValue.attributedText = handlerRichtext(totalProfitLabel, (myWalletaward["totalInterest"].stringValue == "" ? "0.00" : myWalletaward["totalInterest"].stringValue), "元", UIColor.white, UIColor.white, UIColor.white, Font_WL(13), BoldFont_WL(14), BoldFont_WL(14))
                    availableAmountValue.attributedText = handlerRichtext(balanceLabel, "\n", myWalletaward["availableAmount"].stringValue == "" ? "0.00" : myWalletaward["availableAmount"].stringValue, Normal_999Color, Normal_999Color, Normal_333Color, Font_WL(14), Font_WL(14), BoldFont_WL(15))
                    //                availableAmountValue.text = String(format: "账户余额(元)\n%@", myWalletaward["availableAmount"].stringValue == "" ? "0.00" : myWalletaward["availableAmount"].stringValue)
                }else{
                    allAssetsLab.text = "0.00"
                    totalInterestValue.attributedText = handlerRichtext(totalProfitLabel, "0.00", "元", UIColor.white, UIColor.white, UIColor.white, Font_WL(13), BoldFont_WL(14), BoldFont_WL(14))
                    availableAmountValue.attributedText = handlerRichtext(balanceLabel, "\n", "0.00", Normal_999Color, Normal_999Color, Normal_333Color, Font_WL(14), Font_WL(14), BoldFont_WL(15))
                }
            }
        }
       
    }
  
    // 卡片
    lazy var cardImgView: UIImageView = {
        let temp = UIImageView()
        temp.image = UIImage(named: "mine_card_bg")
        return temp
    }()
    // 总资产 值
    lazy var allAssetsLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = UIColor.white
        temp.font = BoldFont_WL(30)
        return temp
    }()
    // 总资产 值
    lazy var allAssetsTextLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = UIColor.white
        temp.font = Font_WL(15)
//        temp.text = "总资产 (元)"
        return temp
    }()
    // 隐藏按钮
    lazy var eyeBtn: UIButton = {
        let temp = UIButton(type: UIButtonType.custom)
        temp.isSelected = UserDefaults.standard.bool(forKey: "isBiyan")
        temp.setImage(UIImage(named: "mine_eyeopen"), for: .normal)
        temp.setImage(UIImage(named: "mine_eyeclose"), for: .selected)
        temp.addTarget(self, action: #selector(openOrCloseEye), for: .touchUpInside)
        return temp
    }()
    // 竖直白线
    lazy var lineView: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.clear
        return temp
    }()
    // 累计收益 值
    lazy var totalInterestValue: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = UIColor.white
        temp.font = BoldFont_WL(14)
        return temp
    }()
    // 累计收益 文字
    lazy var totalInterestLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .right
        temp.textColor = UIColor.white
        temp.font = Font_WL(13)
//        temp.text = "累计收益 (元):"
        return temp
    }()
    // 账户余额 值
    lazy var availableAmountValue: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = Normal_333Color
        temp.font = Font_WL(15)
        temp.numberOfLines = 2
//        temp.setattt
        return temp
    }()
    // 账户余额 文字
    lazy var availableAmountLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = Normal_999Color
        temp.font = Font_WL(14)
//        temp.text = "账户余额 (元)"
        return temp
    }()
    // 充值按钮
    lazy var rechargeBtn: UIButton = {
        let temp = UIButton(type: UIButtonType.custom)
//        temp.setTitle("充值", for: .normal)
        temp.setTitleColor(UIColor.white, for: .normal)
        temp.titleLabel?.font = Font_WL(15)
        temp.backgroundColor = Normal_ThemeColor
        temp.layer.cornerRadius = 5
        return temp
    }()
    // 提现按钮
    lazy var withdrawalBtn: UIButton = {
        let temp = UIButton(type: UIButtonType.custom)
//        temp.setTitle("提现", for: .normal)
        temp.setTitleColor(Normal_ThemeColor, for: .normal)
        temp.titleLabel?.font = Font_WL(15)
        temp.layer.cornerRadius = 5
        temp.layer.borderColor = Normal_ThemeColor.cgColor
        temp.layer.borderWidth = 1
        return temp
    }()

    //实名认证按钮
    lazy var authenticationButton:UIButton = {
        let temp = UIButton(type: UIButtonType.custom)
        temp.backgroundColor = UIColor.white
        //        temp.setTitle("提现", for: .normal)
        temp.contentEdgeInsets = UIEdgeInsetsMake(0,10,0,10)
        temp.imageView?.contentMode = .scaleAspectFit
        return temp
    }()
    
    /// 处理账户余额累计收益等的富文本
    func handlerRichtext(_ str1: String, _ str2: String,_ str3: String, _ color1: UIColor, _ color2: UIColor, _ color3: UIColor, _ font1:UIFont, _ font2:UIFont, _ font3:UIFont) -> NSMutableAttributedString {
        
        
        let temp = NSMutableAttributedString(string: str1, attributes: [NSForegroundColorAttributeName: color1,NSFontAttributeName:font1])
        temp.append(NSAttributedString(string: str2, attributes: [NSForegroundColorAttributeName: color2,NSFontAttributeName:font2]))
        temp.append(NSAttributedString(string: str3, attributes: [NSForegroundColorAttributeName: color3,NSFontAttributeName:font3]))
        
        // 行间距
//        NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
//        para.lineSpacing = 6;
       
        
        return temp
    }
}
